package com.socket;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import org.xsocket.connection.INonBlockingConnection;

import com.util.Constant;
import com.util.DBStatus;
import com.util.Util;

public class SendMessageToClient extends Thread {
	private boolean start = true;

	public void setStart(boolean start) {
		this.start = start;
	}

	public boolean isStart() {
		return start;
	}

	public void stopSendMessageToClient() {// save msgList To DB
	}

	String message = null;
	private String cmd = null;

	public SendMessageToClient() {
	}

	public void run() {
		while (start) {
			if (DASocketServer.msgList.isEmpty()) {
				try {
					sleep(300);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} else {
				while ((message = DASocketServer.msgList.remove(0)) != null) {
					cmd = message.substring(0, Constant.CMD_LENGTH);
					if (cmd.equals(Constant.CMD_SEND_TO_USER)) {
						sendMessageToUser(message);
					} else if (cmd.equals(Constant.CMD_SEND_TO_GROUP)) {
						sendMessageToGroup(message);
					} else if (cmd.equals(Constant.CMD_LOGIN)) {
					} else if (cmd.equals(Constant.CMD_LOGOUT)) {
					} else if (cmd.equals(Constant.CMD_REGIST)) {
					}
				}
			}
			this.message = null;
		}
	}// end run()

	private void sendMessageToUser(String msg) {
		if (msg.length() > Constant.MIN_MESSAGE_LENGTH) {
			String toUserID = msg.substring(Constant.TO_USER_ID_BEGIN,
					Constant.TO_USER_ID_END);
			INonBlockingConnection connection = (INonBlockingConnection) DASocketServer.userHash
					.get(toUserID);
			if (connection != null) {// user is on line
				try {
					connection.write(Util.getUTFByteBuffer(message));
				} catch (IOException e) {
					// TODO Auto-generated catch block
					saveMessageToDB(msg);
					e.printStackTrace();
				}
			} else { // user is not on line,save message to db
				saveMessageToDB(msg);
			}// end if-else
		}
	}// end sendMessageToUser

	private void saveMessageToDB(String msg) {

		String fromUserID = msg.substring(Constant.FROM_USER_ID_BEGIN,
				Constant.FROM_USER_ID_END);

		String toUserID = msg.substring(Constant.TO_USER_ID_BEGIN,
				Constant.TO_USER_ID_END);
		String datetime = Util.getNowDatetime();

		Connection conn = DBConnection.getConnection();
		PreparedStatement pstmt = null;
		String sql = "insert into daim_message set "
				+ "(FROM_USER_ID,TO_USER_ID,CONTENT,SEND_DATE,STATUS,TYPE)"
				+ " values (?,?,?,?,?,?)";
		try {
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, fromUserID);
			pstmt.setString(2, toUserID);
			pstmt.setString(3, message);
			pstmt.setString(4, datetime);
			pstmt.setInt(5, DBStatus.DAIM_MESSAGE_SEND);
			pstmt.setInt(6, DBStatus.DAIM_MESSAGE_TYPE_MESSAGE);
			pstmt.execute();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if (pstmt != null) {
					pstmt.close();
				}
				if (conn != null) {
					conn.close();
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}// end saveMessageToDB

	private void sendMessageToGroup(String message) {
	}// end sendMessageToGroup
}